package com.test.controller;

import com.test.pojo.AdminUser;
import com.test.service.AdminUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author suxuexia
 * @create 2019-10-07 22:16
 */
@Controller
public class AdminUserCtrl {
    @Resource
    AdminUserService adminUserService;

    @RequestMapping(value="/testSpring",method = RequestMethod.GET)
    public ModelAndView testSpring(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","这是第一个SpringMVC程序");
        mav.setViewName("forward:index.jsp");
        return mav;
    }

    // 实现管理员登录
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(AdminUser adminUser, HttpSession session, Model model) {
        // 进行登录判断
        AdminUser resUser = adminUserService.doLogin(adminUser.getAdminName(),adminUser.getAdminPassword());

        if (resUser != null) {
            session.setAttribute("adminUser",adminUser.getAdminName());

            //return "/WEB-INF/views/FoodTypeList.jsp";
            return "redirect:/getAllFoodType";
           // return "aa";
        }else{
            model.addAttribute("msg","请核对用户名或密码");
            // 加上forward是为了不加上前后缀
            return "forward:login.jsp";
        }
    }
}
